﻿#ifndef CKBRD_H
#define CKBRD_H

#include <QtGui/QMainWindow>
#include <QSystemTrayIcon>
#include <QList>

#include "ui_Vkbrd.h"
#include "SequenceViewWindow.h"
#include "KeyProfile.h"
#include "DGlove.h"


namespace  vkbrd
{
	class User;
	class SequenceWidget;

	class Vkbrd : public QMainWindow
	{
		Q_OBJECT

	public:
		Vkbrd(QWidget *parent = 0, Qt::WFlags flags = 0);

		void setupMenuOptions();

		void connectSignals();

		void setupTrayIcon();

		KeyProfile* getCurrentProfile()
		{
			return ((KeyProfile*) (ui.profiles->currentItem()->data(Qt::UserRole).value<void*>()));
		}

		~Vkbrd();

	protected:

		void closeEvent(QCloseEvent *event) override;

	signals:

		void gloveActiveStateChanged(int _gloveNum, bool _state);

		void sequenceFired(Sequence _seq, bool start);

	private slots:

		void iconActivated(QSystemTrayIcon::ActivationReason reason);
		void showMessage();
		void messageClicked();
		void errorMessageWindow(QString& _text, QString& _informativeText);
		void showWindow();
		void exitProgram();
		void showSequenceWindow();
		void addNewSequence();
		void gloveActivatedDeactivated();
		//void gloveDataAquired(int _gloveNum, int _fingerNum, float _val);
		void addProfile();

		void fingerStateChanged(int _gloveNum, BoolFingers _val);

		void fingerRawDataUpdate(int _gloveNum, gIntFingers _fingerData);

		void saveConfig();

		void selectProfile(QListWidgetItem* _selectedProfile);

		void showCalibrateWindow();

		void calibrateSequence(BoolFingers _seq);

		void resetCalibration();

		void resendKeys();

		void toggleResendKeys(bool _val);

		void deleteActiveProfile();

		/**
		* Refresh key profile by values of sequenceWidgets.
		* Discards zero sequences.
		*/
		void profileSequenceChanged();

		void profileNameEdited(QString _newName);

	private:
		Ui::VkbrdClass			ui;
		QSystemTrayIcon			*trayIcon;
		QMenu					*trayIconMenu;
		QList<QProgressBar*>	fingerBars;
		QList<QLabel*>			messageLabels;
		QList<SequenceWidget*>  sequenceWidgets;
		QList<QCheckBox*>		gloveActiveCheckBoxes;
		QList<QLineEdit*>		portNames;
		SequenceViewWindow		sequenceViewWindow;

		QAction					switchUserAction;
		QAction					showWindowAction;
		QAction					exitProgramAction;
		QAction					showSequenceWindowAction;

		DG5Glove				gloves[2];
		BoolFingers				currentSequence;

		QList<Sequence>			firedSequences;

		bool					ifResendKeys;


		void readConfig();

		void sendInput( Sequence &matchingSequence, bool _keyDown );
	};

} //namespace  vkbrd

#endif // CKBRD_H
